package 栈;
//刘超
public class seqStack implements IStack{
    private final int defaultSize =10;
    private int maxSize;
    int top; //栈顶指示
    Object[] stack; //一堆数据
    public seqStack(){
        initiate(defaultSize);
    }
    public  seqStack(int sz){
        initiate(sz);
    }
    private void initiate(int sz){
        maxSize=sz;
        stack = new Object[sz];
        //置栈为空
        top=-1;
    }
    @Override
    public void push(Object obj) throws Exception {
        if(top==maxSize-1){
            System.out.println("满了");
        }else{
            top++;
            stack[top]=obj;
        }
    }

    @Override
    public Object pop() throws Exception {
        if (top==-1){
            return null;
        }else{
            Object a = stack[top];
            top--;
            return a;
        }
    }

    @Override
    public Object getTop() throws Exception {
        if(top==-1){
            return null;
        }
        return stack[top];
    }

    @Override
    public boolean isEmpty() {
        return top==-1;
    }

    @Override
    public int size() {
        return top+1;
    }

    @Override
    public void print() {
        for (int i = 0; i <=top ; i++) {
            System.out.println(stack[i]+"");
            System.out.println();
        }
    }
}
